package 尚硅谷juc大厂.test7;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 买票案例：50张票
 * 使用ReentrantLock的非公平锁，能够保证线程安全
 */
public class Ticket {

    private int number=50;

    private Lock lock=new ReentrantLock(true);

    public void sale(){
        lock.lock();
        try{
            if (number>0){
                System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--)+"\t还剩"+number);
            }
        }catch (Exception exception){

        }finally {
            lock.unlock();
        }
    }
}
